require(["esri/map",
    "dojo/on",
    "dojo/dom",
    "esri/layers/ArcGISDynamicMapServiceLayer",
    "esri/tasks/RouteTask",
    "esri/tasks/FeatureSet",
    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/TextSymbol",
    "esri/toolbars/draw",
    "esri/symbols/SimpleLineSymbol",
    "esri/tasks/RouteParameters",
    "dojo/colors",
    "esri/graphic",
    "dojo/domReady!"],
    function (Map, on, dom,
        ArcGISDynamicMapServiceLayer,
        RouteTask,
        FeatureSet,
        SimpleMarkerSymbol,
        TextSymbol,
        Draw,
        SimpleLineSymbol,
        RouteParameters,
        Color,
        Graphic
    ) {
        var map = new Map("MyMapDiv");
        var layer = new ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/webgis/fzkt_streets_ND/MapServer");
        map.addLayer(layer)
        //创建路径分析对象
        var shortestAnalyst = new RouteTask("http://localhost:6080/arcgis/rest/services/webgis/fzkt_streets_ND/NAServer/路径");
        //创建路径参数对象
        var routeParas = new RouteParameters();
        //障碍点
        routeParas.barriers = new FeatureSet();
        //停靠点
        routeParas.stops = new FeatureSet();
        routeParas.returnDirections = false;
        routeParas.returnRoutes = true;
        routeParas.outSpatialReference = map.SpatialReference;
        //selectPointID=1说明是添加停靠点
        //selectPointID=2说明是添加障碍点
        var selectPointID;
        on(dom.byId("stop1"), "click", function () {
            selectPointID = 1;
        })
        on(dom.byId("barriers1"), "click", function () {
            selectPointID = 2;
        })
        //停靠点的符号
        var stopSymbol = new SimpleMarkerSymbol();
        stopSymbol.style = SimpleMarkerSymbol.STYLE_CROSS;
        stopSymbol.setSize(15);
        //障碍点的符号
        var barrierSymbol = new SimpleMarkerSymbol();
        barrierSymbol.style = SimpleMarkerSymbol.STYLE_CIRCLE;
        barrierSymbol.setSize(14);
        barrierSymbol.setColor(new Color("#f1a340"));

        on(map, "mouse-down", function (evt) {
            switch (selectPointID) {
                case 0:
                    break;
                case 1:
                    var pointStop = evt.mapPoint;
                    var gr = new Graphic(pointStop, stopSymbol);
                    routeParas.stops.features.push(gr);
                    break;
                case 2:
                    var pointBarrier = evt.mapPoint;
                    var gr = new Graphic(pointBarrier, barrierSymbol);
                    routeParas.barriers.features.push(gr);
                    break;
            }
            if (selectPointID != 0) {
                addPoint(pointStop, stopSymbol)
                addTextPoint("障碍点", pointBarrier, barrierSymbol);
                selectPointID = 0;
            }
        });
        //单一
        function addPoint (point, symbol) {
            var graphicpoint = new Graphic(point, symbol);
            map.graphics.add(graphicpoint);
        }
        //文本
        function addTextPoint (text, point, symbol) {
            var textSymbol = new TextSymbol(text);
            textSymbol.setColor(new Color([128, 0, 0]));
            var graphicText = Graphic(point, textSymbol);
            var graphicpoint = new Graphic(point, symbol);
            map.graphics.add(graphicpoint);
            map.graphics.add(graphicText);
        }
        on(dom.byId("analyse1"), "click", function () {
            //如果障碍点或者停靠点的个数有一个为0，提示用户参数输入不对
            if (routeParas.stops.features.length == 0) {
                alert("ERROR");
                return;
            }
            //执行路径分析函数
            shortestAnalyst.solve(routeParas, showRoute)
        })
        //处理路径分析返回的结果。
        function showRoute (solveResult) {
            console.log(solveResult)
            //路径分析的结果
            var routeResults = solveResult.routeResults;
            //路径分析的长度
            var res = routeResults.length;
            //路径的符号
            routeSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([0, 0, 255, 0.5]), 3);
            if (res > 0) {
                for (var i = 0; i < res; i++) {
                    var graphicroute = routeResults[i];
                    var graphic = graphicroute.route;
                    graphic.setSymbol(routeSymbol);
                    map.graphics.add(graphic);
                }
            }
            else {
                alert("Null");
            }
        }
    });